home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
redakcyjne
/
programy
/
Gadu-Gadu 8.0 Build 9103
/
nowegg.exe
/
scripts
/
common.js
next >
Wrap
Text (UTF-16)
|
2009-07-08
|
10KB
|
214 lines
function urlencode(plaintext)
{
var safechars = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
var hex = "0123456789ABCDEF";
var encoded = "";
for (var i = 0; i < plaintext.length; i++)
{
var ch = plaintext.charAt(i);
if (ch == " ")
{
encoded += "+";
}
else if (safechars.indexOf(ch) != -1)
{
encoded += ch;
}
else
{
var charCode = ch.charCodeAt(0);
if (charCode > 255)
{
encoded += "+";
}
else
{
encoded += "%";
encoded += hex.charAt((charCode >> 4) & 0xF);
encoded += hex.charAt(charCode & 0xF);
}
}
}
return encoded;
};
function urldecode(encoded)
{
var hexchars = "0123456789ABCDEFabcdef";
var plaintext = "";
var i = 0;
while (i < encoded.length)
{
var ch = encoded.charAt(i);
if (ch == "+")
{
plaintext += " ";
i++;
}
else if (ch == "%")
{
if (i < (encoded.length-2) && hexchars.indexOf(encoded.charAt(i+1)) != -1 && hexchars.indexOf(encoded.charAt(i+2)) != -1 )
{
plaintext += unescape(encoded.substring(i,3));
i += 3;
}
else
{
plaintext += "%[ERROR]";
i++;
}
}
else
{
plaintext += ch;
i++;
}
}
return plaintext;
};
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encodeBase64(input)
{
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
do
{
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2))
{
enc3 = enc4 = 64;
}
else if (isNaN(chr3))
{
enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
} while (i < input.length);
return output;
}
function decodeBase64(input)
{
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do
{
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64)
{
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64)
{
output = output + String.fromCharCode(chr3);
}
} while (i < input.length);
return output;
};
/**
* \brief
* Usuwa z numeru telefonu + i kod kierunkowy kraju
*/
function FlattenPhoneNumber(nr, countryCode)
{
var retnr = "";
for (var i = 0; i < nr.length; i++ )
if (('0') <= nr[i] && nr[i] <= ('9'))
retnr = retnr + nr[i];
if (retnr.substring(0, countryCode.length) == countryCode)
{
retnr = retnr.substring(countryCode.length, retnr.length); ///< odrzucamy kod kraju na poczatku
}
return retnr;
};
/**
* \brief
* Funkcja konstruuje stringa który docelowo wysyłany jest w http post jako dane wypełnionego formularza.
* Przykład:
* Przerabia tablice: { "id", "2", "nr", "600123456" }
* na stringa: id=2&nr=600123456
*/
function CreateHTTPRequestContent(requestArray)
{
if (requestArray.length % 2 != 0)
{
return ""; ///< wymagamy tablicy klucz -> wartosc, tak wiec chcemy miec parzysta liczbe elementow
}
var retString = "";
var cur = 0;
while (cur < requestArray.length)
{
if (cur > 0)
{
retString += "&";
}
retString += requestArray[cur] + "=" + requestArray[cur + 1];
cur += 2;
}
return retString;
};
/*
* /brief
* Funkcja konvertuje stringa do uinta
*/
function toInt(str)
{
var i = 0;
var ret = 0;
var cyfry = "0123456789";
var pos = 0;
while (i < str.length)
{
pos = cyfry.search(str.substring(i, i+1));
if (pos < 0)
break;
ret = 10 * ret + pos;
i++;
}
return ret;
};